﻿using System;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.DoiForm
{
    public partial class frmDoiDetail : frmDetail
    {
        private readonly IService<Phong> _phong;
        private readonly IService<Doi> _doiService;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        private readonly IUserService<aspnet_Users> _userService;

        public frmDoiDetail()
        {
            InitializeComponent();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Đội";
            LoadPhong();
            cboPhong.Focus();
        }

        public frmDoiDetail(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Đội";
            LoadPhong();
            cboPhong.Focus();
        }

        private void LoadPhong()
        {
            cboPhong.DataSource = _phong.ToList().ToList().Select(p=>new {p.Id,TenPhong=p.Ma+" - "+p.TenPhong}).ToList();
            cboPhong.DisplayMember = "TenPhong";
            cboPhong.ValueMember = "Id";
        }
        
        public frmDoiDetail(Guid id)
        {
            InitializeComponent();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doiService = Program.Container.Resolve<IService<Doi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Đội";
            LoadPhong();
            cboPhong.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _doiService.Detail(id);
            if (item != null)
            {
                txtTenDoi.Text = item.TenDoi;
                txtDienGiai.Text = item.DienGiai;
                cboPhong.SelectedValue = item.Phong_Id;
            }
        }
        
        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(cboPhong.SelectedItem.ToString()))
            {
                errorProvider.SetError(cboPhong, "Bạn chưa nhập phòng. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (string.IsNullOrEmpty(txtTenDoi.Text))
            {
                errorProvider.SetError(txtTenDoi, "Bạn chưa nhập tên đội. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new Doi
                {
                    Id = Guid.NewGuid(),
                    TenDoi = txtTenDoi.Text,
                    DienGiai = txtDienGiai.Text,
                    Phong_Id = Guid.Parse(cboPhong.SelectedValue.ToString()),
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _doiService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    this._frmPhieuDieuTraCs.LoadDoi();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _doiService.Detail(guid);
            if (item != null)
            {
                item.TenDoi = txtTenDoi.Text;
                item.DienGiai = txtDienGiai.Text;
                item.Phong_Id = Guid.Parse(cboPhong.SelectedValue.ToString());
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _doiService.Update(item);
            }
        }

        #endregion
    }
}
